using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class SettingPanel : MonoBehaviour
{
    public Toggle bgmToggle, effectToggle;

    public Slider bgmSlider, effectSlider;
    public TextMeshProUGUI bgmValue, effectValue;

    public Button confirmBtn;

    private void Awake()
    {
        var setting = GameController.Instance().playerDataCtr.setting;
        bgmToggle.isOn = setting.BgMState;
        effectToggle.isOn = setting.EffectState;
        bgmSlider.value = setting.BgMValue;
        effectSlider.value = setting.EffectValue;
        bgmValue.text = setting.BgMValue.ToString();
        effectValue.text = setting.EffectValue.ToString();

        confirmBtn.onClick.AddListener(OnClickConfirmBtn);
        Time.timeScale = 0;
    }

    private void OnDestroy()
    {
        confirmBtn.onClick.RemoveListener(OnClickConfirmBtn);
        Time.timeScale = 1;
    }

    private void OnClickConfirmBtn()
    {
        UpdateSettingJson();
        GameController.Instance().audioCtr.SetAllAudio(bgmToggle.isOn, effectToggle.isOn, (int) bgmSlider.value,
            (int) effectSlider.value);
        Destroy(gameObject);
    }

    private void UpdateSettingJson()
    {
        var setting = GameController.Instance().playerDataCtr.setting;
        setting.BgMState = bgmToggle.isOn;
        setting.EffectState = effectToggle.isOn;
        setting.BgMValue = (int) bgmSlider.value;
        setting.EffectValue = (int) effectSlider.value;
        GameController.Instance().playerDataCtr.SaveSettingJson(setting);
    }

    public void SetBGMValueText()
    {
        bgmValue.text = bgmSlider.value.ToString();
    }

    public void SetEffectValueText()
    {
        effectValue.text = effectSlider.value.ToString();
    }
}